import datetime
import json
import requests
import time

from Base.BaseSetting import url_path
from Base.PublicFunction import Requst


class WoniuInterface:
    def __init__(self):
        self.session = Requst.get_session()


    def addApply(self,work_id,start_time,end_time,accounting_way,hours,region_id=1):  # 新增加班申请
        url = url_path +'/WoniuBoss4.0/apply/saveApply'
        data = f'phone=&apply.work_id={work_id}&apply.start_time={start_time}%3A30&apply.end_time={end_time}%3A30&apply.accounting_way={accounting_way}&apply.hours={hours}%E5%B0%8F%E6%97%B6&apply.region_id={region_id}&apply.reason=%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A'
        # print(data)
        header = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        respObj = self.session.post(url=url,data=data,headers=header)
        content = respObj.text
        status_code = respObj.status_code
        return content,status_code

    def queryApply(self,pageSize,pageIndex,status,regionId,start_time,end_time):    # 查询加班申请
        url = url_path +'/WoniuBoss4.0/apply/queryApply'
        data = f'pageSize={pageSize}&pageIndex={pageIndex}&status={status}&regionId={regionId}&code=&sTime={start_time}&eTime={end_time}'
        header = {'Content-Type':'application/x-www-form-urlencoded'}
        respObj = self.session.post(url=url,data=data,headers=header)
        content = respObj.text
        status_code = respObj.status_code
        # print(content)
        dictData = json.loads(content)
        listInfo = dictData['list']
        regionList = []           # 筛选后的地区列表
        statusList = []           # 筛选后的状态列表
        for row in listInfo:
            region = row['region_name']
            status = row['status']
            regionList.append(region)
            statusList.append(status)
        return regionList,statusList,status_code


    def assignWork(self,work_id,date):                       # 指定值班
        url = url_path +'/WoniuBoss4.0/duty/saveDuty'
        data = 'arr=%5B%7B%22work_id%22%3A%22{work_id}%22%2C%22duty_time%22%3A%22{date}%22%7D%5D'
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        respObj = self.session.post(url=url,data=data,headers=header)
        content = respObj.text
        status_code = respObj.status_code
        # print(content)
        return content,status_code

    def queryDuty(self,pageSize,pageIndex,code):            # 查询指定值班信息
        url = url_path +'/WoniuBoss4.0/duty/queryDuty'
        data = f'pageSize={pageSize}&pageIndex={pageIndex}&startTime=&endTime=&regionId=&logStatus=&code={code}'
        header = {'Content-Type':'application/x-www-form-urlencoded'}
        respObj = self.session.post(url=url,data=data,headers=header)
        content = respObj.text
        status_code = respObj.status_code
        dictData = json.loads(content)
        listData = dictData['list']
        id = listData[0]['id']
        return id,status_code

    def editDuty(self):                # 编辑指定值班信息
        url = url_path +'/WoniuBoss4.0/duty/saveEdit'
        duty_time = (datetime.datetime.now() + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
        data = f'duty.id=1026&duty.work_id=WNCD004&duty.duty_time={duty_time}'
        # print(data)
        header = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        respObj = self.session.post(url=url,data=data,headers=header)
        content = respObj.text
        status_code = respObj.status_code
        return content,status_code

if __name__ == '__main__':
    wn = WoniuInterface()
    # wn.addApply()
    # wn.queryApply()
    # # wn.cancelApply()
    # wn.assignWork()
    # wn.queryDuty('3')
    # wn.editDuty('2020-11-29')